<?php

	class Push{
		
		/**
		 * @author Louis Wu
		 * @update Sean Hsu
		 * 將手動推播內容,推播至Android裝置
		 */
		public function android($message, $registrationIDsArray)
		{	
			/* Replace with real server API key from Google APIs  */
			$apiKey = "AIzaSyCUYbNhHTQaIz_CqWwUzn8Aw9iqRnsD7Cs";
			$message = mb_substr($message, 0, 35, 'utf8');
	        
	        /* Set POST variables */
	        $url = 'https://android.googleapis.com/gcm/send';
	
	        $fields = json_encode(array(
		    	'registration_ids' 	=> $registrationIDsArray,
		        'data' 				=> array( "message" => $message )
	        ));
	            			
	        $headers = array(
		    	'Authorization: key=' . $apiKey,
		    	'Content-Type: application/json'
	        );
	
	        // Open connection
	        $ch = curl_init();
	
	        // Set the url, number of POST vars, POST data
	        curl_setopt( $ch, CURLOPT_URL, $url );
	        curl_setopt( $ch, CURLOPT_POST, true );
	        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
	        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
	
	        // Execute post
	        $result = curl_exec($ch);
	
	        // Close connection
	        curl_close($ch);
	        return $result;
			
		}
		
		/**
		 * @author Louis Wu
		 * 將手動推播內容,推播至iOS裝置
		 */
		public function ios($message, $device)
		{
	
//			$pemPath 		= $_SERVER ['DOCUMENT_ROOT'] . "";
			$pemPath 		= $_SERVER ['DOCUMENT_ROOT'] . "/payment/library/util/apns-dev-test.pem";
			$pemPath 		= $_SERVER ['DOCUMENT_ROOT'] . "/payment/library/util/apns-dev.pem";
			$pemPassword 	= "1234";
			
			$devices = array((object)array("serno"=>$device, "test"=>1));
			$message = mb_substr($message, 0, 35, 'utf8')."...";
			
			$ctx = stream_context_create();
			stream_context_set_option($ctx, "ssl", "local_cert", $pemPath);
			stream_context_set_option($ctx, 'ssl', 'passphrase', $pemPassword);
			
			//此处有两个服务器需要选择，如果是开发测试用，选择第二名sandbox的服务器并使用Dev的pem证书，如果是正是发布，使用Product的pem并选用正式的服务器
			$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
			//$fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
			
			if (!$fp) {
			    return $err;
		    } 	
			 
			foreach($devices as $item) {
				if ($item->test ==0) {
					continue;
				}
				try{ 
					//$deviceToken = "d3586370e3c76c723384f1c127bf2fbc6e8320dfae3300098f8e6aadeeeb651d";
					$body = array(
									"aps" => array(
														"alert" 	=> "$message", 
														"badge" 	=> 1, 
														"sound" 	=> "default"
													)
					);
				 
					$payload = json_encode($body);
				 
					$msg = chr(0).pack("n",32).pack("H*", str_replace(' ', "", $item->serno)).pack("n",strlen($payload)).$payload;
				
				 	fwrite($fp, $msg);
				 	
				}catch(Exception $e){
					return $e->getMessage();
				}			 
			}
			fclose($fp);
			return true;
		}
	}
?>